<?php

$rt = \Phpcmf\Service::M('comment', 'comment')->install_module('wenda', SITE_ID);
if (!$rt['code']) {
    log_message('error', '评论系统插件：'.$rt['msg']);
} else {
    $config = [
        'use' => 1,
        'cname' => '答案',
        'ct_reply' => 1,
        'pagesize' => 20,
        'pagesize_api' => 20,
        'pagesize_mobile' => 20,
        'order' => 'inputtime asc',
    ];
    $ct = \Phpcmf\Service::M()->table('app_comment')->where('name', 'module')->getRow();
    if ($ct) {
        $ct_cfg = dr_string2array($ct['value']);
        $ct_cfg['wenda'] = $config;
        \Phpcmf\Service::M()->db->table('app_comment')->where('name', 'module')->update([
            'value' => dr_array2string($ct_cfg),
        ]);
    } else {
        \Phpcmf\Service::M()->db->table('app_comment')->insert([
            'name' => 'module',
            'value' => dr_array2string([
                'wenda' => $config,
            ]),
        ]);
    }

    /*if (\Phpcmf\Service::M()->is_table_exists(SITE_ID.'_wenda_comment')) {
        $sql = 'INSERT INTO `{dbprefix}field` (`id`, `name`, `fieldname`, `fieldtype`, `relatedid`, `relatedname`, `isedit`, `ismain`, `issystem`, `ismember`, `issearch`, `disabled`, `setting`, `displayorder`) VALUES
    (null, \'最佳答案\', \'zuijiadaan\', \'Text\', 0, \'comment-module-wenda\', 1, 1, 0, 0, 0, 0, \'{"option":{"fieldtype":"","fieldlength":"","value":"","width":"","css":""},"validate":{"required":"0","pattern":"","errortips":"","check":"","filter":"","formattr":"","tips":""},"is_right":"0"}\', 0);
    ';
        \Phpcmf\Service::M()->query_all($sql);
        if (!\Phpcmf\Service::M()->is_field_exists(SITE_ID.'_wenda_comment', 'zuijiadaan')) {
            \Phpcmf\Service::M()->query_all('ALTER TABLE `{dbprefix}'.SITE_ID.'_wenda_comment` ADD `zuijiadaan` INT(10) NULL;');
        }
    }*/
	$sql = 'INSERT INTO `{dbprefix}field` (`id`, `name`, `fieldname`, `fieldtype`, `relatedid`, `relatedname`, `isedit`, `ismain`, `issystem`, `ismember`, `issearch`, `disabled`, `setting`, `displayorder`) VALUES
    (null, \'最佳答案\', \'zuijiadaan\', \'Text\', 0, \'comment-module-wenda\', 1, 1, 0, 0, 0, 0, \'{"option":{"fieldtype":"","fieldlength":"","value":"","width":"","css":""},"validate":{"required":"0","pattern":"","errortips":"","check":"","filter":"","formattr":"","tips":""},"is_right":"0"}\', 0);
    ';
	\Phpcmf\Service::M()->query_all($sql);
	if (!\Phpcmf\Service::M()->is_field_exists(SITE_ID.'_wenda_comment', 'zuijiadaan')) {
		\Phpcmf\Service::M()->query_all('ALTER TABLE `{dbprefix}'.SITE_ID.'_wenda_comment` ADD `zuijiadaan` INT(10) NULL;');
	}
	$sql_tu = 'INSERT INTO `{dbprefix}field` (`id`, `name`, `fieldname`, `fieldtype`, `relatedid`, `relatedname`, `isedit`, `ismain`, `issystem`, `ismember`, `issearch`, `disabled`, `setting`, `displayorder`) VALUES
	(null, \'答案图\', \'huidatu\', \'Files\', 0, \'comment-module-wenda\', 1, 1, 0, 1, 0, 0, \'{"option":{"size":"100","count":"9","ext":"jpg,gif,png","attachment":"0","image_reduce":"1000","is_ext_tips":"0","css":""},"validate":{"xss":"1","required":"0","pattern":"","errortips":"","check":"","filter":"","tips":"","formattr":""},"is_right":"0"}\', 0);
	';
	\Phpcmf\Service::M()->query_all($sql_tu);
	if (!\Phpcmf\Service::M()->is_field_exists(SITE_ID.'_wenda_comment', 'huidatu')) {
		\Phpcmf\Service::M()->query_all('ALTER TABLE `{dbprefix}'.SITE_ID.'_wenda_comment` ADD `huidatu` TEXT COLLATE utf8mb4_unicode_ci COMMENT \'内容图\';');
	}
}


